<?php
namespace backend\widgets;
use Yii;

class ToChineseNumber
{
    private $money = "";
    private $cnynums = array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖");
    private $cnyunits = array("圆", "角", "分");
    private $cnygrees = array("拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟");

    function __construct($money = 0)
    {
        $this->money = round($money, 2);
    }

    private function cnyMapUnit($list, $units)
    {
        $ul = count($units);
        $xs = array();
        $list = array_reverse($list);var_dump($list);var_dump(each($list));var_dump($ul);
        while (list($keys, $vals) = each($list)) {
            $l = count($xs);var_dump($l);//exit;
            if ($vals != "0" || !($l % 4)) {
                $val = $vals . ($units[($l - 1) % $ul]);
            } else {
                $val = is_numeric($xs[0][0]) ? $vals : '';
            }
            array_unshift($xs, $val);
        }
        return $xs;
    }

    public function arr($leng = 0, $suffix = false)
    {
        /*if(strstr($this->money,'.')){
            $money1 = $this->money;
        }else{
            $money1 = $this->money .'.00';
        }
        $this->money = $money1;*/

        list($int, $dec) = explode(".", $this->money, 2);
        $cnygrees = $this->cnygrees;
        $cnynums = array_keys($this->cnynums);
        $temp = array();
        $index = 0;
        if ($leng > 0 && $suffix !== false) {
            for ($i = 0; $i < $leng; $i++) {
                $cnygrees[$i] = $suffix;
            }
        }
        var_dump($int);
        var_dump(str_split($int));
        var_dump($dec);
        var_dump($cnygrees);

        $data = $this->cnyMapUnit(str_split($int), $cnygrees);
        $data = array_reverse($data);
        while (list($key, $val) = each($data)) {
            $val = str_replace($cnynums, $this->cnynums, $val);
            if (empty($temp[$index])) {
                $temp[$index] = '';
            }
            $temp[$index] = $val . $temp[$index];
            $index++;
            if ($index > $leng) {
                $index = $leng;
            }
        }
        return array_reverse($temp);
    }
}